package com.gxuwz.weather.citymanager;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.gxuwz.weather.R;
import com.gxuwz.weather.adapter.CityDeliteAdpter;
import com.gxuwz.weather.db.DBHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CityDeleteActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView error, delete;
    private ListView deleteLv;
    private DBHelper dbHelper;
    private List<Map<String, String>> myAreaData;
    private CityDeliteAdpter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_delete);

        error = findViewById(R.id.delete_iv_error);
        delete = findViewById(R.id.delete_iv_right);
        deleteLv = findViewById(R.id.delete_lv);

        dbHelper = new DBHelper(this);

        // 加载 myarea 表中的数据
        myAreaData = loadMyAreaData();
        adapter = new CityDeliteAdpter(this, myAreaData);
        deleteLv.setAdapter(adapter);

        error.setOnClickListener(this);
        delete.setOnClickListener(this);

        // 设置列表项点击事件
        deleteLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 切换选中状态
                adapter.toggleSelection(position);
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.delete_iv_error:
                intent.setClass(this, CityManagerActivity.class);
                startActivity(intent);
                break;
            case R.id.delete_iv_right:
                deleteSelectedCities();
                break;
        }
    }

    @SuppressLint("Range")
    private List<Map<String, String>> loadMyAreaData() {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query("myarea", new String[]{"areaCn", "cityCn", "areaId", "areaCode"}, null, null, null, null, null);

        List<Map<String, String>> resultList = new ArrayList<>();
        while (cursor.moveToNext()) {
            Map<String, String> cityInfo = new HashMap<>();
            cityInfo.put("areaCn", cursor.getString(cursor.getColumnIndex("areaCn")));
            cityInfo.put("cityCn", cursor.getString(cursor.getColumnIndex("cityCn")));
            cityInfo.put("areaId", cursor.getString(cursor.getColumnIndex("areaId")));
            cityInfo.put("areaCode", cursor.getString(cursor.getColumnIndex("areaCode")));
            resultList.add(cityInfo);
        }
        cursor.close();
        return resultList;
    }

    private void deleteSelectedCities() {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        List<Map<String, String>> selectedCities = adapter.getSelectedItems();

        for (Map<String, String> city : selectedCities) {
            String areaId = city.get("areaId");
            db.delete("myarea", "areaId = ?", new String[]{areaId});
        }

        // 重新加载数据
        myAreaData = loadMyAreaData();
        adapter.updateData(myAreaData);
    }
}
